package com.kunya.product.controller.portall;

import com.kunya.common.common.Const;
import com.kunya.common.common.ServerResponse;
import com.kunya.common.util.CommonUtil;
import com.kunya.product.service.cart.ICartService;
import com.kunya.product.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zp
 * @Description: 购物车controller
 * @date 2018/5/25 14:28
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private ICartService iCartService;

    /**
     * @param userId
     * @author zp
     * @Description: 获取购物车列表
     * @date 2018/5/28 16:07
     */
    @RequestMapping("/list")
    public ServerResponse<CartVo> list(HttpServletRequest httpServletRequest,Long userId) {
        return iCartService.list(userId,CommonUtil.getTablePiefix(httpServletRequest));
    }

    /**
     * @param count 添加数量 userId 用户id productSpecNumber 商品规格属性编号
     * @author zp
     * @Description: 向购物车中添加商品
     * @date 2018/5/28 16:07
     */
    @RequestMapping("/add")
    public ServerResponse<CartVo> add(HttpServletRequest httpServletRequest,Integer count, Long userId, Long productSpecNumber) {
        return iCartService.add(userId, productSpecNumber, count,CommonUtil.getTablePiefix(httpServletRequest));
    }

    @RequestMapping("/update")
    public ServerResponse<CartVo> update(Long userId,Integer count,Long productSpecNumber,HttpServletRequest httpServletRequest) {
        return iCartService.update(userId, productSpecNumber, count,CommonUtil.getTablePiefix(httpServletRequest));
    }
    @RequestMapping("/delete_product")
    public ServerResponse<CartVo> deleteProduct(Long userId,String productSpecNumbers,HttpServletRequest httpServletRequest) {
        return iCartService.deleteProduct(userId,productSpecNumbers,CommonUtil.getTablePiefix(httpServletRequest));
    }

    @RequestMapping("/select_all")
    public ServerResponse<CartVo> selectAll(Long userId,HttpServletRequest httpServletRequest) {
        return iCartService.selectOrUnSelect(userId,null, Const.Cart.CHECKED,CommonUtil.getTablePiefix(httpServletRequest));
    }

    @RequestMapping("/un_select_all")
    public ServerResponse<CartVo> unSelectAll(Long userId,HttpServletRequest httpServletRequest) {
        return iCartService.selectOrUnSelect(userId, null,Const.Cart.UN_CHECKED,CommonUtil.getTablePiefix(httpServletRequest));
    }

    @RequestMapping("/select")
    @ResponseBody
    public ServerResponse<CartVo> select(Long userId,Long productSpecNumber,HttpServletRequest httpServletRequest) {
        return iCartService.selectOrUnSelect(userId, productSpecNumber,Const.Cart.CHECKED,CommonUtil.getTablePiefix(httpServletRequest));
    }

    @RequestMapping("/un_select")
    @ResponseBody
    public ServerResponse<CartVo> unSelect(Long userId,Long productSpecNumber,HttpServletRequest httpServletRequest) {
        return iCartService.selectOrUnSelect(userId, productSpecNumber,Const.Cart.UN_CHECKED,CommonUtil.getTablePiefix(httpServletRequest));
    }

    @RequestMapping("/get_cart_product_count")
    public ServerResponse<Integer> getCartProductCount(Long userId,HttpServletRequest httpServletRequest) {
        return iCartService.getCartProductCount(userId, CommonUtil.getTablePiefix(httpServletRequest));
    }

}
